package com.mobor.moborsvr.infra.search.impl;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.mobor.moborsvr.infra.search.SearchRequestManager;

/**
 * Implementation of {@link SearchRequestManager}} interface.
 * @author teroleg
 *
 */
@Component
public class SearchRequestManagerImpl implements SearchRequestManager {
	
	private static final Logger logger = LogManager.getLogger(SearchRequestManagerImpl.class.getName());

	private static ExecutorService executorService;
	private static Integer DEFAULT_POOL_SIZE = 10;
	
	public SearchRequestManagerImpl(){
		
	}
	
	@PostConstruct
	public void init(){
		logger.debug("Initializing executor service");
		
		if(executorService == null)
			executorService = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);		
	}
	
	@PreDestroy
	public void destroy(){
		logger.debug("Shutting down executor service");
		
		executorService.shutdown();
	}
	
	public Future<List<?>> executeSearch(Callable<List<?>> worker){
		logger.debug("Starting to execute new search");
		
		return executorService.submit(worker);
	}	
}
